package com.tyc.jmh;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

/**
 * 类描述
 *
 * @author tyc
 * @version 1.0
 * @date 2023-03-08 13:56:49
 */
public class Example_01_HelloJMH {

    /**
     * 任何被测函数都需要使用@Benchmark来标注。JMH框架会调用被@Benchmark标注的函数，并自动统计它们的性能指标
     * @return
     */
    @Benchmark
    @OutputTimeUnit(TimeUnit.SECONDS)
    @BenchmarkMode(Mode.SampleTime)
    public String sayHello() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(10);
        return "hello jmh";
    }

    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(Example_01_HelloJMH.class.getSimpleName())
                .forks(1)
                .build();
        new Runner(options).run();
    }
}
